"""Helper functions

Consists of functions to typically be used within templates, but also
available to Controllers. This module is available to both as 'h'.
"""
#from webhelpers import *
from webhelpers.html import escape, HTML, literal, url_escape
from routes.util import url_for

from email.header import decode_header

def header_print(s):
    dh = decode_header(s)
    res = u''
    for p,e in dh:
        if e == None:
            e = 'US-ASCII'
        res += unicode(p, e)
    return escape(res)

def body_print(m):
    db = m.get_payload(decode=True)
    res = u''
    d = m.get_param('charset')
    if type(d) == type(''):
        res = unicode(db, d)
    elif type(d) == type(tuple()):
        c, l, v % d
        res = unicode(db, v)
    else:
        res = unicode(db, 'US-ASCII')
    return res
